package anthology.daily_question._25_01;

import java.util.HashMap;
import java.util.Map;

//https://leetcode.cn/problems/contains-duplicate-ii/description/?envType=daily-question&envId=2025-01-29
public class _0219存在重复元素2 {
    class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            int length = nums.length;
            for (int i = 0; i < length; i++) {
                int num = nums[i];
                if (map.containsKey(num) && i - map.get(num) <= k) {
                    return true;
                }
                map.put(num, i);
            }
            return false;
        }
    }
}
